package com.hot.hot_backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hot.hot_backend.pojo.HotGoods;
import com.hot.hot_backend.pojo.HotMessage;
import com.hot.hot_backend.pojo.Vo;
import com.hot.hot_backend.service.impl.HotGoodsServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  食材
 * </p>
 *
 * @author hot
 * @since 2024-07-17
 */
@CrossOrigin
@RestController
@RequestMapping("/hot_goods")
public class HotGoodsController {

    @Resource
    private HotGoodsServiceImpl goodsService;

    // 新增食材
    @PostMapping("/insert")
    public Vo<HotGoods> insert(@RequestBody HotGoods goods){
        boolean b = goodsService.save(goods);
        if (b){
            return new Vo<>(b,"新增成功！");
        }else {
            return new Vo<>(b,"新增失败，请稍后重试！");
        }
    }

    // 删除食材
    @GetMapping("delete/{id}")
    public Vo<HotGoods> delete(@PathVariable("id") String id){
        UpdateWrapper<HotGoods> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        boolean b = goodsService.remove(wrapper);
        if (b){
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }



    // 修改
    @PostMapping("/update")
    public Vo<HotGoods> update(@RequestBody HotGoods goods){
        boolean b = goodsService.updateById(goods);
        if (b){
            return new Vo<>(b,"修改成功！");
        }else {
            return new Vo<>(b,"修改失败，请稍后重试!");
        }
    }

    // 分类查询
    @GetMapping("/queryType/{current}/{type}")
    public Vo<HotGoods> queryType(@PathVariable("current") Integer current, @PathVariable("type") String type) {
        LambdaQueryWrapper<HotGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(type),HotGoods::getType, type);
        Page<HotGoods> goodsPage = new Page<>(current <= 0 ? 1 : current, 6);
        Page<HotGoods> page = goodsService.page(goodsPage, wrapper);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "该分类还没有食材！");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }

    // 分页查询
    @GetMapping("/page/{current}")
    public Vo<HotGoods> page(@PathVariable("current") Integer current) {
        Page<HotGoods> goodsPage = new Page<>(current <= 0 ? 1 : current, 6);
        IPage<HotGoods> page = goodsService.page(goodsPage);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "当前并没有食材！");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }
    // 搜索查询
    @GetMapping("/search/{current}/{name}")
    public Vo<HotGoods> search(@PathVariable("current") Integer current, @PathVariable("name") String name) {
        LambdaQueryWrapper<HotGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(name),HotGoods::getName, name);
        Page<HotGoods> goodsPage = new Page<>(current <= 0 ? 1 : current, 6);
        Page<HotGoods> page = goodsService.page(goodsPage, wrapper);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "并没有该结果");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }
}
